home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 11 / CU Amiga Magazine's Super CD-ROM 11 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-06].iso / cucd / graphics / mpimage / si / unpacker.c < prev    next >
C/C++ Source or Header  |  1995-04-01  |  2KB  |  73 lines

  1.  
  2. #include "iffp/ilbm.h"
  3. #include "iffp/packer.h"
  4.  
  5. /*----------------------------------------------------------------------*
  6.  * unpacker.c Convert data from "cmpByteRun1" run compression. 11/15/85
  7.  *
  8.  * Based on code by Jerry Morrison and Steve Shaw, Electronic Arts.
  9.  * This software is in the public domain.
  10.  *
  11.  *    control bytes:
  12.  *     [0..127]   : followed by n+1 bytes of data.
  13.  *     [-1..-127] : followed by byte to be repeated (-n)+1 times.
  14.  *     -128       : NOOP.
  15.  *
  16.  * This version for the Commodore-Amiga computer.
  17.  *----------------------------------------------------------------------*/
  18.  
  19. /*----------- UnPackRow ------------------------------------------------*/
  20.  
  21. #define UGetByte()    (*source++)
  22. #define UPutByte(c)    (*dest++ = (c))
  23.  
  24. /* Given POINTERS to POINTER variables, unpacks one row, updating the source
  25.  * and destination pointers until it produces dstBytes bytes.
  26.  */
  27.  
  28. BOOL unpackrow(BYTE **pSource, BYTE **pDest, WORD srcBytes0, WORD dstBytes0)
  29.     {
  30.     register BYTE *source = *pSource;
  31.     register BYTE *dest   = *pDest;
  32.     register WORD n;
  33.     register WORD srcBytes = srcBytes0;
  34.     register WORD dstBytes = dstBytes0;
  35.     BOOL error = TRUE;    /* assume error until we make it through the loop */
  36.     WORD minus128 = -128;  /* get the compiler to generate a CMP.W */
  37.     register BYTE c;
  38.  
  39.     while( dstBytes > 0 )  {
  40.     if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  41.         n = UGetByte();
  42.  
  43.         if (n >= 0) {
  44.         n += 1;
  45.         if ( (srcBytes -= n) < 0 )  goto ErrorExit;
  46.         if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  47.         do {  UPutByte(UGetByte());  } while (--n > 0);
  48.         }
  49.  
  50.         else if (n != minus128) {
  51.         n = -n + 1;
  52.         if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  53.         if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  54.         c = UGetByte();
  55.         do {  UPutByte(c);  } while (--n > 0);
  56.         }
  57.     }
  58.     error = FALSE;    /* success! */
  59.  
  60.   ErrorExit:
  61.     *pSource = source;  *pDest = dest;
  62.  
  63. #ifdef MYDEBUG
  64.     if(error)
  65.     D(bug("unpackrow exit: srcBytes=%ld->%ld dstBytes=%ld->%ld n=%ld error=%ld\n",
  66.         srcBytes0, srcBytes, dstBytes0, dstBytes, n, error));
  67. #endif
  68.     return(error);
  69.     }
  70.  
  71.  
  72. /* end */
  73.